﻿Imports System.IO

Public Class _Default
    Inherits System.Web.UI.Page

    Private Const strImgMedia As String = "<img src=""./" & "Icons/media32.png"" alt=""Video"" title=""Video"" />"
    Private Const strImgArchive As String = "<img src=""./" & "Icons/archive32.png"" alt=""Archiv"" title=""Archiv"" />"
    Private Const strImgPicture As String = "<img src=""./" & "Icons/pic32.png"" alt=""Bild"" title=""Bild"" />"
    Private Const strImgUnknown As String = "<img src=""./" & "Icons/multi32.png"" alt=""Unbekannter Dateityp"" title=""Unbekannter Dateityp"" />"

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim objDirectory As New DirectoryInfo(Server.MapPath(""))

        LabelHeader.Text = Server.HtmlEncode("/" & objDirectory.Name.Substring(0, 1).ToUpper & objDirectory.Name.Substring(1))
        Me.Title = LabelHeader.Text

        Dim objHeaderRow As New TableRow
        Dim objHeaderIcon As New TableCell
        Dim objHeaderName As New TableCell
        Dim objHeaderSize As New TableCell
        Dim objHeaderDate As New TableCell
        objHeaderIcon.Text = "<b>Typ</b>"
        objHeaderName.Text = "<b>Name</b>"
        objHeaderSize.Text = "<b>" & Server.HtmlEncode("Größe") & "</b>"
        objHeaderDate.Text = "<b>" & Server.HtmlEncode("Änderungsdatum") & "</b>"
        objHeaderRow.Cells.Add(objHeaderIcon)
        objHeaderRow.Cells.Add(objHeaderName)
        objHeaderRow.Cells.Add(objHeaderSize)
        objHeaderRow.Cells.Add(objHeaderDate)
        TableContent.Rows.Add(objHeaderRow)

        For Each objFile As FileInfo In objDirectory.GetFiles
            If Not ForbiddenFileType(objFile.Name) Then
                Dim objTableRow As New TableRow

                Dim objTableCellIcon As New TableCell
                Select Case GetFileType(objFile.Name)
                    Case FileType.Media
                        objTableCellIcon.Text = strImgMedia
                    Case FileType.Archive
                        objTableCellIcon.Text = strImgArchive
                    Case FileType.Picture
                        objTableCellIcon.Text = strImgPicture
                    Case FileType.Unknown
                        objTableCellIcon.Text = strImgUnknown
                    Case Else
                        objTableCellIcon.Text = strImgUnknown
                End Select

                Dim objTableCellText As New TableCell
                objTableCellText.Text = "<a href=""./" & Server.HtmlEncode(objFile.Name) & """>" & objFile.Name & "</a>" & "<br />"

                Dim objTableCellSize As New TableCell
                Dim intFileBytes As Long = objFile.Length
                Select Case intFileBytes
                    Case Is > (1024 ^ 3) 'GB
                        objTableCellSize.Text = (intFileBytes / (1024 ^ 3)).ToString("0.##") & " GB"
                    Case Is > (1024 ^ 2) 'MB
                        objTableCellSize.Text = (intFileBytes / (1024 ^ 2)).ToString("0.##") & " MB"
                    Case Else 'KB
                        objTableCellSize.Text = (intFileBytes / 1024).ToString("0.##") & " KB"
                End Select

                Dim objTableCellLastChanged As New TableCell
                objTableCellLastChanged.Text = objFile.LastWriteTime.ToString

                objTableRow.Cells.Add(objTableCellIcon)
                objTableRow.Cells.Add(objTableCellText)
                objTableRow.Cells.Add(objTableCellSize)
                objTableRow.Cells.Add(objTableCellLastChanged)
                TableContent.Rows.Add(objTableRow)

            End If
        Next

        LabelServerInfo.Text = Server.MachineName & "<br />" & _
            My.Computer.Info.OSFullName & " " & My.Computer.Info.OSVersion & " (" & My.Computer.Info.OSPlatform & ") " & My.Computer.Info.InstalledUICulture.ToString & "<br />" & _
            Now.ToLongDateString & ", " & Now.ToLongTimeString & " (UTC)"
    End Sub

    Private Enum FileType As Integer
        Unknown = 0
        Media = 1
        Archive = 2
        Picture = 3
    End Enum

    Private Function GetFileType(ByVal FileName As String) As FileType
        Dim objReturnValue As FileType
        If FileName.ToLower.EndsWith("avi") Or _
            FileName.ToLower.EndsWith("mpg") Or _
            FileName.ToLower.EndsWith("mpeg") Then
            objReturnValue = FileType.Media
        ElseIf FileName.ToLower.EndsWith("rar") Or _
            FileName.ToLower.EndsWith("zip") Or _
            FileName.ToLower.EndsWith("7z") Then
            objReturnValue = FileType.Archive
        ElseIf FileName.ToLower.EndsWith("png") Or _
            FileName.ToLower.EndsWith("jpg") Or _
            FileName.ToLower.EndsWith("jpeg") Or _
            FileName.ToLower.EndsWith("tif") Or _
            FileName.ToLower.EndsWith("tiff") Or _
            FileName.ToLower.EndsWith("bmp") Or _
            FileName.ToLower.EndsWith("gif") Then
            objReturnValue = FileType.Picture
        Else
            objReturnValue = FileType.Unknown
        End If
        Return objReturnValue
    End Function

    Private Function ForbiddenFileType(ByVal FileName As String) As Boolean
        Dim bolReturnValue As Boolean = False
        If FileName.ToLower.EndsWith("aspx") Or _
            FileName.ToLower.EndsWith("config") Then
            bolReturnValue = True
        End If
        Return bolReturnValue
    End Function

End Class